package com.sinodata.bsm.cicp.collector.windows.snmp;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.snmp4j.CommunityTarget;
import org.snmp4j.Snmp;

import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.snmp.GetData;
import com.sinodata.bsm.cicp.util.snmp.MibDiskAndMemoEntry;
import com.sinodata.bsm.cicp.util.snmp.OMMappingInfo;
import com.sinodata.bsm.common.vo.ResPropertyValue;
import com.sinodata.bsm.common.vo.ResPropertyValue.PropValue;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-8-28 上午11:08:13          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class DiskCollector extends SnmpCollector {

    private static final Logger logger = Logger.getLogger(DiskCollector.class);
    private static final DecimalFormat format = new DecimalFormat("#.##");

    @Override
    public String[] getPropMeta() {
        //总磁盘空间,可用磁盘空间,磁盘使用率
        return new String[] { "$1", "$2", "$3" };
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "diskName" };
    }

    @Override
    public List<ResPropertyValue> collect(Snmp snmp, CommunityTarget target) throws CollectorException {
        /**
         * 总磁盘空间,单位GB
         */
        long sumDiskSize = -1;

        /**
         * 可用磁盘空间,单位GB
         */
        long freeDiskSize = -1;

        /**
         * 当剩余空间小于1G时，保留两位小数,单位GB
         */
        double freeDiskSizeDouble = -1;

        /**
         * 磁盘使用率
         */
        int diskUseRate = -1;
        List<ResPropertyValue> list = new ArrayList<ResPropertyValue>();
        String[] props = getPropMeta();
        List<OMMappingInfo> disk;
        try {
            disk = GetData.getAllTableData(snmp, MibDiskAndMemoEntry.class, target);
        } catch (Exception e) {
            logger.error(ip + " snmp获取windows磁盘相关数据时出错", e);
            throw new CollectorException(e);
        }
        if (disk.size() < 1) {
            throw new CollectorException(ip + " snmp没有获取到windows磁盘相关数据");
        }
        for (OMMappingInfo mapping : disk) {
            MibDiskAndMemoEntry d = (MibDiskAndMemoEntry) mapping;
            if ("1.3.6.1.2.1.25.2.1.4".equals(d.getHrStorageType())) {
                String diskName = getDiskNameFromDesc(d.getHrStorageDescr());
                sumDiskSize = d.getHrStorageAllocationUnits() * 1L * d.getHrStorageSize() / 1024 / 1024 / 1024;
                freeDiskSize = (d.getHrStorageSize() - d.getHrStorageUsed()) * 1L * d.getHrStorageAllocationUnits() / 1024 / 1024 / 1024;
                if (freeDiskSize == 0) {//剩余空间小于1G
                    freeDiskSizeDouble = (d.getHrStorageSize() - d.getHrStorageUsed()) * 1d * d.getHrStorageAllocationUnits() / 1024 / 1024 / 1024;
                }
                diskUseRate = 100 * d.getHrStorageUsed() / d.getHrStorageSize();

                String key = ip + "_" + diskName;

                ResPropertyValue resPropertyValueX = new ResPropertyValue();
                resPropertyValueX.setInid(key);
                list.add(resPropertyValueX);
                PropValue[] values = new ResPropertyValue.PropValue[props.length];
                resPropertyValueX.setValues(values);
                values[0] = ResPropertyValue.createPropValue(sumDiskSize + "", props[0]);
                values[1] = ResPropertyValue.createPropValue(freeDiskSize == 0 ? format.format(freeDiskSizeDouble) : freeDiskSize + "", props[1]);
                values[2] = ResPropertyValue.createPropValue(diskUseRate + "", props[2]);
            }
        }

        return list;
        //        String diskName = this.getParamMap().get("diskName");
        //        String key = PRIFIX + ip + "_" + diskName;
        //        Vector<String> propValueVector = (Vector<String>) SnmpValueBuffer.getInstance().get(key, TIMEOUT);
        //        if (propValueVector == null) {
        //            synchronized (MyLock.getLock(ip)) {
        //                propValueVector = (Vector<String>) SnmpValueBuffer.getInstance().get(key, TIMEOUT);
        //                if (propValueVector == null) {
        //                    collectData(snmp, target);
        //                }
        //            }
        //        }
        //        if (propValueVector == null) {
        //            return null;
        //        }
        //        List<ResPropertyValue> resultList = new ArrayList<ResPropertyValue>();
        //        String[] props = this.getPropMeta();
        //        ResPropertyValue resPropertyValue = new ResPropertyValue();
        //        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[props.length];
        //        for (int i = 0; i < propValueVector.size(); i++) {
        //            propValues[i] = ResPropertyValue.createPropValue(propValueVector.get(i), props[i]);
        //        }
        //        resPropertyValue.setValues(propValues);
        //        resultList.add(resPropertyValue);
        //
        //        return resultList;
    }

    /**
     * @param snmp
     * @param target
     */
    //    private void collectData(Snmp snmp, CommunityTarget target) throws CollectorException {
    //        logger.info(">>>> " + ip + " 发起了snmp采集磁盘信息");
    //
    //        /**
    //         * 总磁盘空间,单位GB
    //         */
    //        long sumDiskSize = -1;
    //
    //        /**
    //         * 可用磁盘空间,单位GB
    //         */
    //        long freeDiskSize = -1;
    //
    //        /**
    //         * 当剩余空间小于1G时，保留两位小数,单位GB
    //         */
    //        double freeDiskSizeDouble = -1;
    //
    //        /**
    //         * 磁盘使用率
    //         */
    //        int diskUseRate = -1;
    //
    //        List<OMMappingInfo> disk;
    //        try {
    //            disk = GetData.getAllTableData(snmp, MibDiskAndMemoEntry.class, target);
    //        } catch (Exception e) {
    //            logger.error(ip + " snmp获取windows磁盘相关数据时出错", e);
    //            throw new CollectorException(e);
    //        }
    //        if (disk.size() < 1) {
    //            throw new CollectorException(ip + " snmp没有获取到windows磁盘相关数据");
    //        }
    //        for (OMMappingInfo mapping : disk) {
    //            MibDiskAndMemoEntry d = (MibDiskAndMemoEntry) mapping;
    //            if ("1.3.6.1.2.1.25.2.1.4".equals(d.getHrStorageType())) {
    //                Vector<String> propValueVector = new Vector<String>(3);
    //                String diskName = getDiskNameFromDesc(d.getHrStorageDescr());
    //                sumDiskSize = d.getHrStorageAllocationUnits() * 1L * d.getHrStorageSize() / 1024 / 1024 / 1024;
    //                freeDiskSize = (d.getHrStorageSize() - d.getHrStorageUsed()) * 1L * d.getHrStorageAllocationUnits() / 1024 / 1024 / 1024;
    //                if (freeDiskSize == 0) {//剩余空间小于1G
    //                    freeDiskSizeDouble = (d.getHrStorageSize() - d.getHrStorageUsed()) * 1d * d.getHrStorageAllocationUnits() / 1024 / 1024 / 1024;
    //                }
    //                diskUseRate = 100 * d.getHrStorageUsed() / d.getHrStorageSize();
    //                propValueVector.add(sumDiskSize + "");
    //                propValueVector.add(freeDiskSize == 0 ? format.format(freeDiskSizeDouble) : freeDiskSize + "");
    //                propValueVector.add(diskUseRate + "");
    //                String key = PRIFIX + ip + "_" + diskName;
    //                SnmpValueBuffer.getInstance().put(key, propValueVector);
    //            }
    //        }
    //        if (sumDiskSize == -1) {
    //            throw new CollectorException(ip + " snmp采集windows磁盘指标有其他oid，现在程序中使用的oid匹配不成功");
    //        }
    //    }

    private String getDiskNameFromDesc(String storageDesc) {
        int index = storageDesc.indexOf(':');
        if (index < 0) {
            return null;
        }
        String name = storageDesc.substring(0, index);
        return name;
    }

    //    private static class MyLock {
    //
    //        private static final Map<String, Object> lockMap = new HashMap<String, Object>();
    //
    //        public static Object getLock(String key) {
    //            Object lock = lockMap.get(key);
    //            if (lock == null) {
    //                lock = new Object();
    //                lockMap.put(key, lock);
    //            }
    //            return lock;
    //        }
    //    }
}
